package com.wnn.demo.model.hospital.controller;


import com.github.pagehelper.PageInfo;
import com.wnn.demo.domain.Employee;
import com.wnn.demo.model.hospital.service.EmployeeService;
import com.wnn.demo.pojo.Result;
import com.wnn.demo.pojo.ResultCode;
import com.wnn.demo.vo.EmployeeVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 员工管理界面
 */
@RestController
@RequestMapping("/manage/employee")
public class EmployeeController {


    @Autowired
    private EmployeeService employeeService;


    /**
     * @param searchEmployee  模糊搜索 员工姓名、电话号号码、工号
     * @param employeeIdView  员工表主键搜索
     * @param isCheck         员工是否已经和用户表账号管理 0未关联 1关联
     * @param hId              医院ID
     * @param pId
     * @param deptChildId      科室ID
     * @param type
     * @return
     */
    @GetMapping()
    public Result selectEmployeeList(String searchEmployee,String employeeIdView,String isCheck
                                            ,String hId, String pId , String deptChildId, Integer type, @RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer limit) {
        PageInfo<EmployeeVo> pageInfo = employeeService.selectEmployeeList(searchEmployee,employeeIdView,isCheck,hId, pId, deptChildId, type, page, limit);
        return new Result(ResultCode.OK,pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 查找员工个人信息
     */
    @GetMapping("/info")
    public Result findEmployeeInfo() {
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipal();
        Map< String, Object > info = employeeService.findEmployeeInfo(username);
        return new Result(ResultCode.OK, info);
    }


    /**
     *
     * 查找医院下的部门科室
     * @param hId
     * @return
     */
    @GetMapping("/findHospitalAndDept")
    public Result findHospitalAndDept(String hId,Integer type,String pDeptId) {

        List<Map<String,Object>> list = employeeService.findHospitalAndDept(hId,type,pDeptId);
        return new Result(ResultCode.OK,list);

    }



    /**
     * 新增员工
     *
     * @param employeeVo
     * @return
     */
    @PostMapping()
    public Result insertEmployee(@RequestBody EmployeeVo employeeVo) {
        int i = employeeService.insertEmployee(employeeVo);
        if (i > 0) {
            return Result.ok();
        }
        return Result.error();
    }

    /**
     * 更新员工
     *
     * @param employeeVo
     * @return
     */
    @PutMapping("/{id}")
    public Result updateEmployee(@PathVariable(value = "id") String id, @RequestBody EmployeeVo employeeVo) {
        employeeVo.setEmployeeId(id);
        int i = employeeService.updateEmployee(employeeVo);
        if (i > 0) {
            return Result.ok();
        }
        return Result.error();
    }


    /**
     * 根据员工Id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable(value = "id") String id) {
        int i = employeeService.deleteEmployeeById(id);
        if (i > 0) {
            return Result.ok();
        }
        return Result.error();
    }


    /**
     * 批量删除员工信息
     * @param ids
     * @return
     */
    @PostMapping("/batchDelete")
    public Result batchEmployeeByIds(String ids) {

        int i = employeeService.batchEmployeeByIds(ids);
        if (i > 0) {
            return Result.ok();
        }
        return Result.error();

    }

    /**
     * 手写批量删除 范例
     * @param ids
     * @return
     */
    @PostMapping("/testBatchDelete")
    public Result testBatchEmployeeByIds(String ids) {

        int i = employeeService.testBatchEmployeeByIds(ids);
        if (i > 0) {
            return Result.ok();
        }
        return Result.error();

    }


}
